/**
 * Representation d'une transformation <=> d'un changement de repere
 * 
 */

KM.Transform = function( iMat4 )
{
    this._matrix = iMat4;
    
    if( !this._matrix )
    {
        this._matrix = mat4.create();
        mat4.identity( this._matrix );
    }
};

KM.Transform.prototype = {
    constructor: KM.Transform,
    
    setMatrix: function( matrix )
    {
        this._matrix = matrix;
        
        return this;
    },
    
    copyFromTransform: function( iTransform )
    {
        mat4.set( iTransform._matrix, this._matrix );
        
        return this;
    },
    
    multiply: function( transform )
    {
        var oTransform = new KM.Transform();
        
        mat4.multiply( this._matrix, transform._matrix, oTransform._matrix );
        
        return oTransform;
    },
    
    rotate: function( iAngle, iAxis )
    {
        mat4.rotate( this._matrix, iAngle, iAxis );
        
        return this;
    },
    
    translate: function( vec3 )
    {
        mat4.translate( this._matrix, vec3 );
        
        return this;
    },
    
    scale: function( iVec3 )
    {
        mat4.scale( this._matrix, iVec3 );
    },
    
    perspective: function( fov, aspectRatio, near, far )
    {
        this._matrix = mat4.perspective( fov, aspectRatio, near, far );
        
        return this;
    },
    
    lookAt: function( eye, target, up )
    {
        this._matrix = mat4.lookAt( eye, target, up );
        
        return this;
    },
    
    getNormalMatrix: function( nMatrix )
    {
        mat4.identity( nMatrix );
        mat4.inverse( this._matrix, nMatrix );
        mat4.transpose( nMatrix );
        
        return nMatrix;
    },
    
    inverse: function()
    {
        var oTransform = new KM.Transform();
        
        mat4.inverse( this._matrix, oTransform._matrix );
        
        return oTransform;
    },
    
    toString: function()
    {
        return "Transform : \n"
            + "\t matrix => " + mat4.str( this._matrix );
    }
};
